﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string GoodsClass_Add(GoodsClass model, int ACID);


        [OperationContract]
        string GoodsClass_Update(GoodsClass model, int ACID);


        [OperationContract]
        string GoodsClassByClassID_Delete(string ClassID, int ACID);


        [OperationContract]
        GoodsClass GoodsClassByClassID_Get(string ClassID, int ACID);


        [OperationContract]
        List<GoodsClass> GoodsClass_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加GoodsClass表数据
        /// <summary>
        /// 添加GoodsClass
        /// </summary>
        /// <param name="model">GoodsClass实体</param>
        /// <returns>空或错误</returns>
        public string GoodsClass_Add(GoodsClass model, int ACID)
        {
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassName))
            {
                return "『产品类别名称』不能为空！";
            }
            if (model.ClassID==model.ParentID)
            {
                return "【类别编号】不能为【父类编号】！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.GoodsClass.Count(p => p.ClassID == model.ClassID) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.GoodsClass.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加GoodsClass表数据事物用重构
        /// <summary>
        /// 添加GoodsClass事物用重构
        /// </summary>
        /// <param name="model">GoodsClass实体</param>
        /// <returns>空或错误</returns>
        private string GoodsClass_Add(GoodsClass model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassName))
            {
                return "『产品类别名称』不能为空！";
            }
            if (ctx.GoodsClass.Count(p => p.ClassID == model.ClassID) > 0)
            {
                return "主键已存在！";
            }
            if (model.ClassID == model.ParentID)
            {
                return "【类别编号】不能为【父类编号】！";
            }
            ctx.GoodsClass.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改GoodsClass表数据
        /// <summary>
        /// 修改GoodsClass
        /// </summary>
        /// <param name="model">GoodsClass实体</param>
        /// <returns>空或错误</returns>
        public string GoodsClass_Update(GoodsClass model, int ACID)
        {
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassName))
            {
                return "『产品类别名称』不能为空！";
            }
            if (model.ClassID == model.ParentID)
            {
                return "【类别编号】不能为【父类编号】！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.GoodsClass.Count(p => p.ClassID == model.ClassID) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    GoodsClass GoodsClassmodel = ctx.GoodsClass.Single(p => p.ClassID == model.ClassID);
                    if (!string.IsNullOrEmpty(model.ClassID))
                    {
                        GoodsClassmodel.ClassID = model.ClassID;
                    }
                    if (!string.IsNullOrEmpty(model.ClassName))
                    {
                        GoodsClassmodel.ClassName = model.ClassName;
                    }
                    if (!string.IsNullOrEmpty(model.ParentID))
                    {
                        GoodsClassmodel.ParentID = model.ParentID;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除GoodsClass表ByClassID数据
        /// <summary>
        /// 删除GoodsClassByClassID
        /// </summary>
        /// <param name="ClassID">产品类别编号</param>
        /// <returns>空或错误</returns>
        public string GoodsClassByClassID_Delete(string ClassID, int ACID)
        {
            if (string.IsNullOrEmpty(ClassID))
            {
                return "『产品类别编号』不能为空！";
            }

            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (ctx.GoodsClass.Count(p=>p.ParentID.Contains(ClassID))>0)
                {
                    return "此分类子分类，不能被删除！";
                }
                if (ctx.Goods.Count(p=>p.ClassID.StartsWith(ClassID))>0)
                {
                    return "产品分类已使用，不能被删除";
                }
                try
                {
                    if (ctx.GoodsClass.Count(p => p.ClassID == ClassID) != 1)
                    {
                        return null;
                    }
                    GoodsClass model = ctx.GoodsClass.Single(p => p.ClassID == ClassID);
                    ctx.GoodsClass.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除GoodsClass表ByClassID数据[事物用重构]
        /// <summary>
        /// 删除GoodsClassByClassID[事物用重构]
        /// </summary>
        /// <param name="ClassID">产品类别编号</param>
        /// <returns>空或错误</returns>
        private string GoodsClassByClassID_Delete(string ClassID, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            if (ctx.GoodsClass.Count(p => p.ClassID == ClassID) != 1)
            {
                return null;
            }
            GoodsClass model = ctx.GoodsClass.Single(p => p.ClassID == ClassID);
            ctx.GoodsClass.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取GoodsClass表ByClassID数据
        /// <summary>
        /// 获取GoodsClassByClassID
        /// </summary>
        /// <param name="ClassID">产品类别编号</param>
        /// <returns>当前表</returns>
        public GoodsClass GoodsClassByClassID_Get(string ClassID, int ACID)
        {
            if (string.IsNullOrEmpty(ClassID))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.GoodsClass.Count(p => p.ClassID == ClassID) != 1)
                    {
                        return null;
                    }
                    return ctx.GoodsClass.SingleOrDefault(p => p.ClassID == ClassID);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取GoodsClass表数据
        /// <summary>
        /// 获取GoodsClass
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<GoodsClass></returns>
        public List<GoodsClass> GoodsClass_Get(Expression<Func<GoodsClass, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.GoodsClass.ToList();
                        }
                        else
                        {
                            return ctx.GoodsClass.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.GoodsClass.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.GoodsClass.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取GoodsClass表数据
        /// <summary>
        /// 获取GoodsClass
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<GoodsClass></returns>
        public List<GoodsClass> GoodsClass_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return GoodsClass_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取GoodsClass表数据计数
        /// <summary>
        /// 获取GoodsClass表数据计数
        /// </summary>
        /// <returns>List<GoodsClass></returns>
        public int GoodsClass_GetCount(Expression<Func<GoodsClass, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.GoodsClass.Count();

                    }
                    else
                    {
                        return ctx.GoodsClass.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取GoodsClass分页表数据
        /// <summary>
        /// 获取GoodsClass分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<GoodsClass></returns>
        public List<GoodsClass> GoodsClass_Getpage(int pageIndex, int pageSize, Expression<Func<GoodsClass, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.GoodsClass.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.GoodsClass.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.GoodsClass.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.GoodsClass.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改GoodsClass表数据
        /// <summary>
        /// 批量修改GoodsClass
        /// </summary>
        /// <param name="predicate">Expression<Func<GoodsClass, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<GoodsClass,GoodsClass>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int GoodsClass_batchUpdate(Expression<Func<GoodsClass, bool>> predicate, Expression<Func<GoodsClass, GoodsClass>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.GoodsClass.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改GoodsClass表数据事物用重构
        /// <summary>
        /// 批量修改GoodsClass事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<GoodsClass, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<GoodsClass,GoodsClass>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int GoodsClass_batchUpdate(Expression<Func<GoodsClass, bool>> predicate, Expression<Func<GoodsClass, GoodsClass>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.GoodsClass.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除GoodsClass表数据
        /// <summary>
        /// 批量删除GoodsClass
        /// </summary>
        /// <param name="predicate">Expression<Func<GoodsClass, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int GoodsClass_batchDel(Expression<Func<GoodsClass, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.GoodsClass.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除GoodsClass表数据事物用重构
        /// <summary>
        /// 批量删除GoodsClass事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<GoodsClass, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int GoodsClass_batchDel(Expression<Func<GoodsClass, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.GoodsClass.Delete(predicate);
            return ok;
        }
        #endregion


    }
}

